package com.powerteam.controller.sys;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.sys.User;
import com.powerteam.service.sys.UserService;
import com.powerteam.vo.Result;
import com.powerteam.vo.sys.QueryUserVO;
import com.powerteam.vo.sys.UpdatePasswordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController extends AuthorizedController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpSession httpSession;

    /**
     * 用户页
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String user() {

        return "sys/user";
    }

    /**
     * 查询用户
     *
     * @param vo
     * @return PageInfo<User>
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public PageInfo<User> find(@RequestBody QueryUserVO vo) {

        return userService.find(vo);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody User user) {

        return userService.insert(user);
    }

    /**
     * 批量删除用户
     *
     * @param ids
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result delete(@RequestBody List<Integer> ids) {

        return userService.deleteByIds(ids);
    }

    /**
     * 根据用户Id获取用户信息
     *
     * @param user
     * @return User
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public User findById(@RequestBody User user) {

        return userService.findById(user.getUserId());
    }

    /**
     * 更新用户
     *
     * @param user
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody User user) {

        User updateUser = userService.update(user);
        // 如果更新的是当前登录的用户 更新session
        if (super.getUser().getUserId().equals(updateUser.getUserId())) {
            httpSession.setAttribute("User", updateUser);
        }
        return new Result(true);
    }

    /**
     * 更新用户状态 (启用/禁用)
     *
     * @param user
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public Result updateStatus(@RequestBody User user) {

        return userService.updateStatus(user);
    }

    /**
     * 检查用户名是否重复
     *
     * @param user
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/checkUserName", method = RequestMethod.POST)
    public Result checkUserName(@RequestBody User user) {

        return userService.checkUserName(user);
    }

    /**
     * 重置密码
     *
     * @param user
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result resetPassword(@RequestBody User user) {

        return userService.resetPassword(user);
    }

    /**
     * 用户资料
     *
     * @param
     * @return String
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public String profile() {

        return "sys/profile";
    }

    /**
     * 修改密码
     *
     * @param vo
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/12
     */
    @ResponseBody
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public Result updatePassword(@RequestBody UpdatePasswordVO vo) {

        return userService.updatePassword(vo);
    }

}
